
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>回答评论</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travelContent.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/common.js"></script>
    <link href="/css/question/mobile+css+head_mobile+css+ui.css"
          rel="stylesheet" type="text/css">
    <link href="/css/question/new_ask2016.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/common.js"></script>
</head>
<script>
    $(function () {
        var params = getParams();
        if(params.answerId){
            $.get(baseUrl+"/answers/"+params.answerId+"/nums",function (data) {
                $(".comment").renderValues(data,{
                    getHref: function (item, value) {
                        var href = $(item).data("href");
                        $(item).attr("href", href + value);
                    }
                });
            });
            $("#commentForm input[name='answerId']").val(params.answerId);
            var currentPage = 1;//当前页
            var pages;//总页数
            var commentData=[];//定义一个数组用来接收每次分页时的数据
            function query() {//记得传递当前页这个参数
                $.get(baseUrl+"answers/"+params.answerId+"/comments",{currentPge:currentPage},function (data) {
                    $.merge(commentData,data.list);
                    var json = {
                        list: commentData
                    };
                    $("#allComments").renderValues(json);
                    pages = data.pages;
                });
                currentPage ++;
            }
            //默认查询第一页
            query();
            //点击查看更多时查询第二页
            $("#moreComment").click(function () {
                if (currentPage <= pages){//如果当前页数小于等于总页数
                    query();
                }else {
                    $(document).dialog({
                        type : 'notice',
                        content: '<span class="info-text">没有更多评论了!</span>',
                        autoClose: 1000,
                        position: 'bottom'
                    });
                }
            });
        }
        $("#basic-addon2").click(function () {
            if($("#commentText").val() == null || "" == $("#commentText")){
                $(document).dialog({
                    type : 'notice',
                    content: '<span class="info-text">内容不能为空</span>',
                    autoClose: 1000,
                    position: 'bottom'
                });
            }else {
                var user = JSON.parse(sessionStorage.getItem("user"));

                if (user) {
                    var currentUserId = user.id;
                    $(".commentId").val(currentUserId);
                }
                if (user){
                    $("#commentForm").ajaxSubmit({
                        url:baseUrl + "answers/comments/" + params.answerId,
                            success:function (data) {
                            window.location.href = "/answerContent.html?answerId="+params.answerId;
                        }
                    });
                }else {
                    $(document).dialog({
                        type : 'notice',
                        content: '<span class="info-text">请登录过后在评论!</span>',
                        autoClose: 1000,
                        position: 'bottom'
                    });
                }
            }
        });

    });
</script>
<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
            <span>回答评论页面</span>
        </div>
    </div>
</div>
<div class="comment">
    <div class="container">
        <div class="row">
            <div class="col-2 comment-head">
                <a data-href="/userProfiles.html?id=" render-key="respondent.id" render-fun="getHref">
                    <img class="rounded-circle" render-src="respondent.headImgUrl">
                </a>
            </div>
            <div class="col-sm-3">
                <div class="p-sm-3">
                    <span class="comment-star" render-html="respondent.nickName"></span>
                </div>
                <div class="p-sm-3">
                    <span class="comment-star">回答时间</span>
                    <span class="comment-date" render-html="answerTime"></span>
                </div>
            </div>
        </div>
        <div class="comment-content">
            <h5 render-html="answerTitle"></h5>
            <p render-html="answerContent"></p>
        </div>
    </div>
    <div class="count d-flex justify-content-between">
        <div class="p-2">
            <p>
                <i class="fa fa-commenting fa-fw"></i>
                <span render-html="commentNumber"></span>
            </p>
        </div>
    </div>
</div>
<hr/>
<div class="container">
    <h6>全部评论</h6>
    <div id="allComments">
        <div render-loop="list">
            <div class="row">
                <div class="col">
                    <p>
                        <a data-href="userProfiles.html?id=">
                            <img class="rounded-circle head-img" render-src="list.comment.headImgUrl" width="30px">
                            <span render-html="list.comment.nickName"></span>
                        </a>
                    </p>
                    <a >
                        <h6 render-html="list.commentContent"></h6>
                    </a>
                    <p>
                        评论于<span render-html="list.commentTime"></span>
                    </p>
                </div>
            </div>
        </div>
    </div>
    <section class="ans-list a-list">
        <div style="margin-bottom: 65px;" id="moreComment">
            <a class="more-list _j_add_more_button">更多评论</a>
        </div>
    </section>
    <section class="askfoot">
        <form method="post"  id="commentForm">
            <div class="input-group input-group-lg">
                <input type="hidden" name="answerId" value="0">
                <input type="hidden" name="comment.id" value="0" class="commentId">
                <input type="text" class="form-control" name="commentContent" placeholder="说出你的意见"
                       id="commentText" aria-describedby="basic-addon2">
                <span class="input-group-addon btn btn-info btn-lg" id="basic-addon2">评论</span>
            </div>
        </form>
    </section>
</div>
</body>
</html>